/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.bioingenium.kbmed.util.xml;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

/**
 *
 * @author lariverosc
 */
public class DocumentXML {

    private NodeXML root;
    private StringBuffer stringBuffer;
    /**
     * 
     */
    public static final String XML_PATH_SEPARATOR = ":";

    /**
     * @param:
     * @return:
     * @Override
     */
    public DocumentXML(NodeXML root) {
        this.root = root;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public NodeXML getRoot() {
        return root;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public void setRoot(NodeXML root) {
        this.root = root;
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public void add(String xmlPath, String name, String value) {
        String[] tags = xmlPath.split(XML_PATH_SEPARATOR);
        NodeXML current = root;
        if (current.getName().equalsIgnoreCase(tags[0])) {
            for (int i = 1; i < tags.length; i++) {
                if (!current.hasChild(tags[i])) {
                    current.addChild(new NodeXML(tags[i]));
                }
                current = current.getChild(tags[i]);
            }
            current.addChild(new NodeXML(name, value));
        } else {
            System.out.println("Error this is not correct");
        }
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public void add(String xmlPath, Hashtable<String, String> dataTable) {
        String[] tags = xmlPath.split(XML_PATH_SEPARATOR);
        NodeXML current = root;
        if (current.getName().equalsIgnoreCase(tags[0])) {
            for (int i = 1; i < tags.length; i++) {
                if (!current.hasChild(tags[i])) {
                    current.addChild(new NodeXML(tags[i]));
                }
                current = current.getChild(tags[i]);
            }
            String currentKey = "";
            for (Enumeration<String> keys = dataTable.keys(); keys.hasMoreElements();) {
                currentKey = keys.nextElement();
                if (dataTable.get(currentKey).length() > 0) {
                    current.addChild(new NodeXML(currentKey, dataTable.get(currentKey)));
                }
            }
        } else {
            System.out.println("Error this is not correct");
        }
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public NodeXML getNode(String xmlPath) {
        String[] tags = xmlPath.split(XML_PATH_SEPARATOR);
        NodeXML current = root;
        if (current.getName().equalsIgnoreCase(tags[0])) {
            for (int i = 1; i < tags.length; i++) {
                if (current.hasChild(tags[i])) {
                    current = current.getChild(tags[i]);
                } else {
                    return null;
                }
            }
            return current;
        } else {
            return null;
        }
    }

    /**
     * @param:
     * @return:
     * @Override
     */
    public NodeXML getNode(NodeXML initial, String xmlPath) {
        String[] tags = xmlPath.split(XML_PATH_SEPARATOR);
        NodeXML current = initial;
        for (int i = 0; i < tags.length; i++) {
            if (current.hasChild(tags[i])) {
                current = current.getChild(tags[i]);
            } else {
                return null;
            }
        }
        return current;
    }

    @Override
    public String toString() {
        stringBuffer = new StringBuffer();
        return toString(root);
    }

    /**
     * 
     * @param current
     * @return
     */
    public String toString(NodeXML current) {
        stringBuffer.append(current.getName() + " " +current.getValue()+"\n");
        List<NodeXML> childs = current.getChilds();
        if (childs != null) {
            for (NodeXML child : childs) {
                toString(child);
            }
        }
        return stringBuffer.toString();
    }
}
